#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include "bintree.h"

#define MAX_ELEM_NUM	(16)
typedef int BOOL;

BOOL CreateSubTree(pBinTreeNode_t* BinTreeNode, int* ElemArray)
{
	int iRet = 0;
	int index = 0;

	*BinTreeNode = NULL;
	while(ElemArray[index] != '#')
	{
		if (ElemArray[index] != '@')
		{
			*BinTreeNode = (pBinTreeNode_t)malloc(sizeof(BinTreeNode));
			(*BinTreeNode)->Data = ElemArray[index];
			(*BinTreeNode)->pstLChild = NULL;
			(*BinTreeNode)->pstRChild = NULL;
		}
		else
		{

		}
		index++;
	}

	return iRet;
}

int VisitBinTreeFir(pBinTreeNode_t BinTreeNode, int Value)
{
	if (BinTreeNode)
	{
		if (BinTreeNode->Data == Value)
		{
			fprintf(stdout, "%d\n", BinTreeNode->Data);
		}
		else
		{
			VisitBinTreeFir(BinTreeNode->pstLChild, Value);
			VisitBinTreeFir(BinTreeNode->pstRChild, Value);
		}
	}
	return 0;
}

void VisitBinTreeMid(pBinTreeNode_t BinTreeNode)
{
	if (BinTreeNode)
	{
		VisitBinTreeMid(BinTreeNode->pstLChild);
		fprintf(stdout, "%d\n", BinTreeNode->Data);
		VisitBinTreeMid(BinTreeNode->pstRChild);
	}
}

void VisitBinTreeLst(pBinTreeNode_t BinTreeNode)
{
	if (BinTreeNode)
	{
		VisitBinTreeLst(BinTreeNode->pstLChild);
		VisitBinTreeLst(BinTreeNode->pstRChild);
		fprintf(stdout, "%d\n", BinTreeNode->Data);
	}
}

void main(void)
{
	int ElemArray[] =
	{
		1, 2, 3, 4,
		'@', '@', 5, 6, 
		7, '@', '@', '@', 
		'@','@', '@', '@', '#'
	};
	pBinTreeNode_t pstBinTreeRoot = NULL;
	
	CreateSubTree(&pstBinTreeRoot, ElemArray);

	printf("%d\n", VisitBinTreeFir(pstBinTreeRoot, 5));putchar('\n');
	VisitBinTreeMid(pstBinTreeRoot);putchar('\n');
	VisitBinTreeLst(pstBinTreeRoot);putchar('\n');
}